﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ZeroBug.EasyFlow.Core
{
    public class Matrix
    {
        public int RowCount { get; set; }
        public int ColCount { get; set; }
        public MatrixDir Direction { get; set; }
        public SerializableDictionary<string, Variable> Variables { get; set; }
        public Matrix()
        {
            Variables = new SerializableDictionary<string, Variable>();
        }
        public Matrix(int rowCount, int colCount)
            : this()
        {
            this.RowCount = rowCount;
            this.ColCount = colCount;
        }
        public (int, int) GetPoint(int index)
        {
            int row = 0;
            int col = 0;
            switch (this.Direction)
            {
                case MatrixDir.XZ:
                    row = index / this.ColCount;
                    col = index % this.ColCount;
                    break;
                case MatrixDir.XS:
                    row = index / this.ColCount;
                    if (row % 2 == 0)
                        col = index % this.ColCount;
                    else
                        col = this.ColCount - index % this.ColCount - 1;
                    break;
                case MatrixDir.YZ:
                    col = index / this.RowCount;
                    row = index % this.RowCount;
                    break;
                case MatrixDir.YS:
                    col = index / this.RowCount;
                    if (col % 2 == 0)
                        row = index % this.RowCount;
                    else
                        row = this.ColCount - index % this.RowCount - 1;
                    break;
            }
            return (row, col);
        }
        public int GetIndex(string varName, string varValue, AccessType accessType, int index = -1)
        {
            switch(accessType)
            {
                case AccessType.First:
                    for (int i = 0; i < Variables[varName].Count; i++)
                    {
                        if (Variables[varName][i].Equals(varValue))
                            return i;
                    }
                    break;
                case AccessType.FirstNot:
                    for (int i = 0; i < Variables[varName].Count; i++)
                    {
                        if (!Variables[varName][i].Equals(varValue))
                            return i;
                    }
                    break;
                case AccessType.Defined:
                    break;
            }
            return -1;
        }
    }
    public enum MatrixDir
    {
        [Description("XZ")]
        XZ,
        [Description("YZ")]
        YZ,
        [Description("XS")]
        XS,
        [Description("YS")]
        YS
    }
    public enum AccessType
    {
        First,
        FirstNot,
        Defined,
    }
}
